package com.edu.web.servlet;

import com.edu.bean.User;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        System.out.println("进入");
        String userName=req.getParameter("username");
        String userPass=req.getParameter("password");
        System.out.println(userPass);
        UserService userLoginService=new UserServiceImpl();
        User user=new User();
        user.setUsername(userName);
        user.setUserpass(userPass);
        User userN=userLoginService.loginService(user);
        if (userN==null){
            req.setAttribute("msg","账号不存在");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
            return;
        }
        if (userN.getUserId()==-1){
            req.setAttribute("msg","密码错误");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
            return;
        }
        req.setAttribute("user",userN);
        req.getSession().setAttribute("user",userN);
        req.getRequestDispatcher("LoadServlet").forward(req, resp);
        System.out.println("登录成功");
    }
}
